IF EXISTS(SELECT name FROM sysobjects
      WHERE name = 'pInProdPaper_Get' AND type = 'P')
   DROP PROCEDURE pInProdPaper_Get
GO

CREATE PROCEDURE dbo.pInProdPaper_Get
	@vdatFromDate	DATETIME = NULL,
	@vdatToDate		DATETIME = NULL
AS


/*______________________ CONSTANT DECLARATIONS _________________________*/

  

/*______________________ VARIABLE DECLARATIONS _________________________*/

  
/*_______________________________ INIT _________________________________*/


/*________________________ VALIDATE PARAMETERS _________________________*/
	
/*_______________________________ MAIN _________________________________*/
	
	
	SELECT 
		InProdPaper.Id, 
		InProdPaper.[Date],
		TotalMoney = (SELECT SUM(Amount * InputPrice) FROM InProdDetail
						WHERE InProdPaperId = InProdPaper.Id) ,
		TotalAmount = (SELECT SUM(Amount) FROM InProdDetail
						WHERE InProdPaperId = InProdPaper.Id) ,
		RowNumber = ROW_NUMBER() OVER(ORDER BY InProdPaper.Date DESC)
	 FROM 
		InProdPaper
		
	WHERE ((@vdatFromDate IS NULL) OR (DATEDIFF(DAY, @vdatFromDate, InProdPaper.Date) >= 0))
		AND ((@vdatToDate IS NULL) OR (DATEDIFF(DAY, InProdPaper.Date, @vdatToDate) >= 0))
		
	ORDER BY InProdPaper.Date DESC

/*___________________________ ERROR HANDLER ____________________________*/



